#include "stdafx.h"
#include "TcpManager.h"

using namespace System;


int main(array<System::String ^> ^args)
{
	if (args->Length != 2)
	{
		Console::WriteLine("usage: TcpSimulator ip port");
		return -1;
	}
	// ustawienia jezykowe niezalezne od systemu operacyjnego
	// inaczej moga pojawiac sie np. przecinki zamiast kropek w liczbach
	Thread::CurrentThread->CurrentCulture = System::Globalization::CultureInfo::InvariantCulture;	

	Thread^ readKey = gcnew Thread(gcnew ThreadStart(&TcpManager::WaitForKey));
	readKey->Start();

	try
	{

		TcpManager^ tcp = gcnew TcpManager(args[0], args[1]);
		
		TcpManager::TcpMode mode = TcpManager::TcpMode::Server;

		if (mode == TcpManager::TcpMode::Client)
		{
			tcp->Connect();

			while (tcp->SendFrame()>0)
			{			
				Thread::Sleep(125);
			}			
		}
		else
		{
			tcp->Accept();
		}

		tcp->Disconnect();
		

	}
	catch ( ArgumentNullException^ e ) 
	{
		Console::WriteLine( "ArgumentNullException: {0}", e );
	}
	catch ( SocketException^ e ) 
	{
		Console::WriteLine( "SocketException: {0}", e );
	}

	Console::WriteLine( "\n Press Enter to continue..." );
	Console::Read();


	Environment::Exit(0);
}

